/**
 * @Title: IndexController
 * @Package cn.com.artemis.biz.steinsggs.web.controller
 * @Description: TODO
 * @author yulin.xu@downjoy.com
 * @date 2017/6/30 16:48
 * @version V1.0
 */
package cn.com.artemis.biz.steinsggs.web.controller;

import cn.com.artemis.biz.steinsggs.common.util.RedisCache;
import cn.com.artemis.biz.steinsggs.dao.CustomerMapper;
import cn.com.artemis.biz.steinsggs.dao.CustomerQaMapper;
import cn.com.artemis.biz.steinsggs.to.CustomerQaTO;
import cn.com.artemis.biz.steinsggs.to.CustomerTO;
import cn.com.artemis.biz.steinsggs.web.Constants;
import cn.com.artemis.biz.steinsggs.web.RedisKey;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 *
 *@ClassName: IndexController
 * @Description: TODO
 * @author yulin.xu@downjoy.com
 * @date 2017/6/30 16:48
 *
 */
@Controller
public class IndexController implements Constants {

    @Autowired
    private RedisCache redisCache;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private CustomerQaMapper customerQaMapper;

    //home
    @RequestMapping({"/", "/index"})
    public String index() {
        return "index";
    }

    //the-company
    @RequestMapping("/the-company")
    public String theCompany() {
        return "the-company/index";
    }
    @RequestMapping("/the-company/privacy-policy")
    public String privacyPolicy() {
        return "the-company/privacy-policy/index";
    }
    @RequestMapping("/the-company/risk-disclosure")
    public String riskDisclosure() {
        return "the-company/risk-disclosure/index";
    }
    @RequestMapping("/the-company/testimonials")
    public String testimonials() {
        return "the-company/testimonials/index";
    }
    @RequestMapping("/the-company/why-trade")
    public String whyTrade() {
        return "the-company/why-trade/index";
    }

    //services
    @RequestMapping("/services")
    public String services() {
        return "services/index";
    }
    @RequestMapping("/services/managed-futures")
    public String managedFutures() {
        return "services/managed-futures/index";
    }
    @RequestMapping("/services/self-directed")
    public String selfDirected() {
        return "services/self-directed/index";
    }
    @RequestMapping("/services/simulated-trading")
    public String simulatedTrading() {
        return "services/simulated-trading/index";
    }

    //open-account
    @RequestMapping("/open-account")
    public String openAccount() {
        return "open-account/index";
    }

    private String getJessionid(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies.length == 0) {
            return "";
        }
        for (Cookie c:
             cookies) {
            if ("JSESSIONID".equals(c.getName())) {
                return c.getValue();
            }
        }
        return "";
    }

    private void getInfo(Map<String, String> data, HttpServletRequest request) {
        Enumeration<String> enumeration = request.getParameterNames();
        while (enumeration.hasMoreElements()) {
            String name = enumeration.nextElement();
            String value = request.getParameter(name);
            if (StringUtils.isNotEmpty(value)) {
                data.put(name, value);
            }
        }
    }

    private Map<String, String> getData(String jsessionid) {
        Map<String, String> data = null;
        try {
            data = redisCache.getCache(String.format(RedisKey.USER_SESSION.keyformat, jsessionid));
            if (null == data) {
                data = new HashMap<String, String>();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }

    private void saveCustomerData(Map<String, String> data) {

        Integer refCustomerId = null;
        if (StringUtils.isNotEmpty(data.get("lname2"))) {
            CustomerTO refCustomer = new CustomerTO();
            refCustomer.setLastName(data.get("lname2"));
            refCustomer.setFirstName(data.get("fname2"));
            refCustomer.setMiddleName(data.get("mname2"));
            refCustomer.setAddressLine1(data.get("add2"));
            refCustomer.setCity(data.get("city2"));
            refCustomer.setZipCode(data.get("zip2"));
            refCustomer.setCountry(data.get("country2"));
            refCustomer.setDateOfBirth(data.get("bday2"));
            refCustomer.setHomePhone(data.get("homephone2"));
            refCustomer.setWorkPhone(data.get("workphone2"));
            refCustomer.setEmailAddress(data.get("email2"));
            refCustomer.setMobileNumber(data.get("mobile2"));
            refCustomer.setEmploymentStatus(data.get("employment2"));
            refCustomer.setOccupation(data.get("occupation2"));
            refCustomer.setEmployer(data.get("employer2"));
            refCustomer.setWorkAddress(data.get("workadd2"));
            refCustomer.setCreateTime(new Date());
            customerMapper.insert(refCustomer);

            refCustomerId = refCustomer.getId();

            List<CustomerQaTO> list = new ArrayList<CustomerQaTO>();
            CustomerQaTO refCustomerQaTO = new CustomerQaTO();
            refCustomerQaTO.setQuestion("infoa2");
            refCustomerQaTO.setAnswer(data.get("infoa2"));
            list.add(refCustomerQaTO);

            refCustomerQaTO = new CustomerQaTO();
            refCustomerQaTO.setQuestion("infob2");
            refCustomerQaTO.setAnswer(data.get("infob2"));
            list.add(refCustomerQaTO);

            refCustomerQaTO = new CustomerQaTO();
            refCustomerQaTO.setQuestion("infoc2");
            refCustomerQaTO.setAnswer(data.get("infoc2"));
            list.add(refCustomerQaTO);

            for (CustomerQaTO c:
                 list) {
                c.setCustomerId(refCustomer.getId());
                customerQaMapper.insert(c);
            }
        }

        CustomerTO customerTO = new CustomerTO();
        customerTO.setRefId(refCustomerId);
        customerTO.setBrokerName(data.get("brokername"));
        customerTO.setToOpenAccountType(data.get("actyp"));
        customerTO.setLastName(data.get("lname1"));
        customerTO.setFirstName(data.get("fname1"));
        customerTO.setMiddleName(data.get("mname1"));
        customerTO.setAddressLine1(data.get("add1"));
        customerTO.setCity(data.get("city1"));
        customerTO.setZipCode(data.get("zip1"));
        customerTO.setCountry(data.get("country1"));
        customerTO.setDateOfBirth(data.get("bday1"));
        customerTO.setHomePhone(data.get("homephone1"));
        customerTO.setWorkPhone(data.get("workphone1"));
        customerTO.setEmailAddress(data.get("email1"));
        customerTO.setMobileNumber(data.get("mobile1"));
        customerTO.setEmploymentStatus(data.get("employment1"));
        customerTO.setOccupation(data.get("occupation1"));
        customerTO.setEmployer(data.get("employer1"));
        customerTO.setWorkAddress(data.get("workadd1"));
        customerTO.setCreateTime(new Date());
        customerMapper.insert(customerTO);

        List<CustomerQaTO> list = new ArrayList<CustomerQaTO>();
        CustomerQaTO customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("infoa1");
        customerQaTO.setAnswer(data.get("infoa1"));
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("infob1");
        customerQaTO.setAnswer(data.get("infob1"));
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("infoc1");
        customerQaTO.setAnswer(data.get("infoc1"));
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("dividends1");
        customerQaTO.setAnswer(data.get("dividends1"));
        if ("others".equals(data.get("dividends1"))) {
            customerQaTO.setAnswer(data.get("dividends_others"));
        }
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("securities1");
        customerQaTO.setAnswer(data.get("securities1"));
        if ("others".equals(data.get("securities1"))) {
            customerQaTO.setAnswer(data.get("securities_others"));
        }
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("sales1");
        customerQaTO.setAnswer(data.get("sales1"));
        if ("others".equals(data.get("sales1"))) {
            customerQaTO.setAnswer(data.get("sales_others"));
        }
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("objective1");
        customerQaTO.setAnswer(data.get("objective1"));
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("experience1");
        customerQaTO.setAnswer(data.get("experience1"));
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("annual1");
        customerQaTO.setAnswer(data.get("annual1"));
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("networth1");
        customerQaTO.setAnswer(data.get("networth1"));
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("liquid1");
        customerQaTO.setAnswer(data.get("liquid1"));
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("maritalstatus1");
        customerQaTO.setAnswer(data.get("maritalstatus1"));
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("dependents1");
        customerQaTO.setAnswer(data.get("dependents1"));
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("knowledge1");
        customerQaTO.setAnswer(data.get("knowledge1"));
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("trans_securities1");
        customerQaTO.setAnswer(data.get("trans_securities1"));
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("trans_currencies1");
        customerQaTO.setAnswer(data.get("trans_currencies1"));
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("trans_commodities1");
        customerQaTO.setAnswer(data.get("trans_commodities1"));
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("trans_options1");
        customerQaTO.setAnswer(data.get("trans_options1"));
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("trans_futures1");
        customerQaTO.setAnswer(data.get("trans_futures1"));
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("transsize1");
        customerQaTO.setAnswer(data.get("transsize1"));
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("maritalstatus2");
        customerQaTO.setAnswer(data.get("maritalstatus2"));
        list.add(customerQaTO);


        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("dependents2");
        customerQaTO.setAnswer(data.get("dependents2"));
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("knowledge2");
        customerQaTO.setAnswer(data.get("knowledge2"));
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("trans_securities2");
        customerQaTO.setAnswer(data.get("trans_securities2"));
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("trans_currencies2");
        customerQaTO.setAnswer(data.get("trans_currencies2"));
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("trans_commodities2");
        customerQaTO.setAnswer(data.get("trans_commodities2"));
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("trans_options2");
        customerQaTO.setAnswer(data.get("trans_options2"));
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("trans_futures2");
        customerQaTO.setAnswer(data.get("trans_futures2"));
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("transsize2");
        customerQaTO.setAnswer(data.get("transsize2"));
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("confirmation1");
        customerQaTO.setAnswer(data.get("confirmation1"));
        list.add(customerQaTO);

        customerQaTO = new CustomerQaTO();
        customerQaTO.setQuestion("confirm_email");
        customerQaTO.setAnswer(data.get("confirm_email"));
        list.add(customerQaTO);

        for (CustomerQaTO c:
             list) {
            c.setCustomerId(customerTO.getId());
            customerQaMapper.insert(c);
        }
    }

    //online-application
    @RequestMapping("/online-application")
    public String onlineApplication(@RequestParam(required = false) String step, HttpServletRequest request, Model model) {

        String page = "index";
        String jsessionid = getJessionid(request);

        if (StringUtils.isEmpty(step)) {
            redisCache.delKey(String.format(RedisKey.USER_SESSION.keyformat, jsessionid));
        }
        if (StringUtils.isNotEmpty(step)) {
            String go = "step" + step.split(",")[0];
            Map<String, String> data = getData(jsessionid);
            model.addAttribute("data", data);
            if ("step7".equals(go) && step.split(",").length == 1) {
                saveCustomerData(data);
                redisCache.delKey(String.format(RedisKey.USER_SESSION.keyformat, jsessionid));
                go = "last";
            } else {
                getInfo(data, request);
                try {
                    redisCache.putCacheWithExpireTime(String.format(RedisKey.USER_SESSION.keyformat, jsessionid), data, RedisKey.USER_SESSION.expire);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            page = go;
        }
        return "online-application/" + page;
    }

    //investor-education
    @RequestMapping("/investor-education")
    public String investorEducation() {
        return "investor-education/index";
    }
    @RequestMapping("/investor-education/faq")
    public String faq() {
        return "investor-education/faq/index";
    }
    @RequestMapping("/investor-education/financial-futures")
    public String financialFtures() {
        return "investor-education/financial-futures/index";
    }
    @RequestMapping("/investor-education/glossary")
    public String glossary() {
        return "investor-education/glossary/index";
    }
    @RequestMapping("/investor-education/online-system")
    public String onlineSystem() {
        return "investor-education/online-system/index";
    }
    @RequestMapping("/investor-education/options-basics")
    public String optionsBasics() {
        return "investor-education/options-basics/index";
    }
    @RequestMapping("/investor-education/options-strategies")
    public String optionsStrategies() {
        return "investor-education/options-strategies/index";
    }
    @RequestMapping("/investor-education/order-entry")
    public String orderEntry() {
        return "investor-education/order-entry/index";
    }
    @RequestMapping("/investor-education/qualities")
    public String qualities() {
        return "investor-education/qualities/index";
    }

    //contact
    @RequestMapping("/contact")
    public String contact() {
        return "contact/index";
    }



}
